let CardUtil = require("./CardUtil");
let ZhuojiGameConstant = require("../constant/zhuojiGameConstant");
let Random = require('../../../utils/random');
let logger = require("pomelo-logger").getLogger(__filename);

class WashCardRule{
    static getTotalCards() {
        let cards = [];
        this.addBaseCards(cards);
        return cards;
    }

    static washCards() {
        let cards = this.getTotalCards();
        Random.shuffle(cards);
        return cards;
    }

    static addCardsByColor(cards, color){
        for (let i = 1; i <= 9; i++) {
            let card = CardUtil.genCard(color, i);
            cards.push(...[card, card, card, card]);
        }
    }

    static addBaseCards(cards){
        this.addCardsByColor(cards, ZhuojiGameConstant.MAHJONG_CODE_COLOR_WAN);
        this.addCardsByColor(cards, ZhuojiGameConstant.MAHJONG_CODE_COLOR_TIAO);
        this.addCardsByColor(cards, ZhuojiGameConstant.MAHJONG_CODE_COLOR_TONG);
    }

    static doZuopai(){
        let one = [
            0x11
            // 0x11, 0x11, 0x12, 0x12, 0x13, 0x13, 0x14, 0x14 //碰 补杠
            // 0x01, 0x01, 0x01, 0x01 //暗杠
            // 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03  //明杠 暗杠
            // 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x05  //天听
        ];
        let two = [
            0x11, 0x11
        ];
        let three = [

        ];
        let four = [
            // 0x04, 0x04, 0x04, 0x04
        ];
        let grabCard = [
            // 0x05
        ];

        let zuopaiCards = [...one, ...two, ...three, ...four, ...grabCard];

        let cards = [];
        let cardsMap = new Map();
        let totalCards = this.getTotalCards();
        for (let card of totalCards) {
            if(!cardsMap.has(card)) {
                cardsMap.set(card, 1);
            } else {
                cardsMap.set(card, cardsMap.get(card) + 1);
            }
        }
        for (let card of zuopaiCards) {
            if(!cardsMap.has(card)) {
                logger.error(`zuopai error! card:${card} error!`);
                return;
            } else {
                cardsMap.set(card, cardsMap.get(card) - 1);
                if (cardsMap.get(card) < 0) {
                    logger.error(`zuopai error! card:${card} num error!`);
                }
            }
        }
        for (let [card, num] of cardsMap.entries()) {
            for (let i = 0; i < num; i++) {
                cards.push(card);
            }
        }
        Random.shuffle(cards);
        return [...one, ...cards.splice(0, 13 - one.length),
            ...two, ...cards.splice(0, 13 - two.length),
            ...three, ...cards.splice(0, 13 - three.length),
            ...four, ...cards.splice(0, 13 - four.length),
            ...grabCard, ...cards.splice(0, 1 - grabCard.length),
            ...cards
        ];
    }
}

module.exports = WashCardRule;


